package org.csu.xzypetstore.controller;


import org.csu.xzypetstore.domain.Account;
import org.csu.xzypetstore.domain.Order;
import org.csu.xzypetstore.service.orderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/order/")
@SessionAttributes({"orderList","order"})
public class orderController {

    @Autowired
    private orderService orderService;
    @GetMapping("orderList")
    public String orderList(HttpServletRequest request, Model model)
    {
        HttpSession session = request.getSession();
        Account account = (Account)session.getAttribute("account");
        List<Order>orderList = orderService.getOrdersByUsername(account.getUsername());
        model.addAttribute("orderList",orderList);

        return "order/listOrder";
    }

    @GetMapping("checkOrder")
    public String checkOrder(HttpServletRequest request,Model model)
    {
        HttpSession session = request.getSession();
        Integer orderId = Integer.valueOf(request.getParameter("orderId"));
        Order order = orderService.getOrder(orderId);
        model.addAttribute("order",order);
        return "order/viewOrder";
    }
}
